var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('upload2Lqy', { title: '测试上传灵雀云' });
});

router.post('/buildConfig',function(req,res,next) {
    let method = "POST";
    let data = {
        name: "demo_test_1",
        space_name: "jiangnan",
        image_cache_enabled: true,
        auto_build_enabled: true,
        auto_tag_type: "TIME",
        build_image_enabled: true,
        code_repo: {
            code_repo_client: "SIMPLE_GIT",
            code_repo_path: "https://gitee.com/Mercy4/Upload2LQY.git",
            code_repo_type: "BRANCH",
            code_repo_type_value: "master",
            build_context_path: "/",
            dockerfile_location: "/"
        },
        customize_tag: "lastest",
        image_repo: {
            name: "upload2lqytesting",
            registry: {
                name: "alauda_public_registry"
            }
        }
    };

    let urltemp = "http://18.1.103.19:20081/v1/private-build-configs/alauda/?namespace=alauda";
    let theurl = url.parse(urltemp);

    if (method == "POST") {
        let postData = data;
        let stringData = JSON.stringify(postData);
        let options = {
            hostname: theurl.hostname,
            port: theurl.port,
            path: theurl.path,
            method: "POST",
            headers: {
                "Authorization": "token xxxxxxxxd",
                "Content-Type": "appliction/json"
            }
        };
    }
    ;
    let theReq = http.request(options, function (theReq) {

        console.log('状态码:${theReq.statusCode}');
        console.log('响应头:&{JSON.stringify(theRes.headers)}');
        theRes.setEncoding('utf8');
        theRes.on('data', (chunk) => {
            console.log('响应主体:${chunk}');
            respData += chunk;
        });
        theRes.on('end', function () {
            console.log('响应中已无数据');
            console.log(respData);
            res.send(respData);

        });

    });
    theReq.on('error', function (e) {
        console.error('请求遇到问题post:${e.messege}')
    });
theReq.write(stringData);
theReq.end();
});

module.exports = router;